﻿using AutoMapper;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.IRepository;
using JuCheap.Core.IService;
using JuCheap.Core.Model;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JuCheap.Core.Service.AppService
{
    /// <summary>
    /// 部门扩展服务
    /// </summary>
    public class DepartmentService : IDepartmentService
    {
        private readonly ISysDepartmentRepository _sysDepartmentRepository;
        private readonly ISysCompanyRepository _sysCompanyRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// ctor
        /// </summary>
        public DepartmentService(ISysDepartmentRepository sysDepartmentRepository, IMapper mapper,
            ISysCompanyRepository sysCompanyRepository)
        {
            _sysDepartmentRepository = sysDepartmentRepository;
            _sysCompanyRepository = sysCompanyRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取部门表格树状结构
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public async Task<IList<SysDepartmentDto>> GetTableTreeAsync(SysDepartmentFilter filter)
        {
            var query = from department in _sysDepartmentRepository.Queryable().WhereIf(filter.Name.IsNotBlank(), x => x.Name.Contains(filter.Name))
                        join company in _sysCompanyRepository.Queryable() on department.CompanyId equals company.Id
                        select new SysDepartmentDto
                        {
                            Id = department.Id,
                            Name = department.Name,
                            ParentId = department.ParentId,
                            CreateTime = department.CreateTime,
                            Remark = department.Remark,
                            CompanyName = company.CompanyName
                        };

            var departments = await query.ToListAsync();
            var tree = DoGetTableTreeChildren(string.Empty, departments);
            return tree;
        }

        /// <summary>
        /// 递归获取部门树
        /// </summary>
        /// <param name="parentId">父级id</param>
        /// <param name="list">部门原始列表</param>
        /// <returns></returns>
        private List<SysDepartmentDto> DoGetTableTreeChildren(string parentId, IEnumerable<SysDepartmentDto> list)
        {
            var children = list
                .WhereIf(parentId.IsBlank(), x => x.ParentId.IsBlank())
                .WhereIf(parentId.IsNotBlank(), x => x.ParentId == parentId)
                .ToList();
            foreach (var tree in children)
            {
                tree.Children = DoGetTableTreeChildren(tree.Id, list);
                if (!tree.Children.AnyOne())
                {
                    tree.Children = null;
                }
            }
            return children;
        }

        /// <summary>
        /// 加载部门树状结构
        /// </summary>
        /// <returns></returns>
        public async Task<List<SelectTreeResponseDto>> GetSimpleTreeAsync()
        {
            var departments = await _sysDepartmentRepository.Queryable()
                .Select(x => new SelectTreeResponseDto
                {
                    Id = x.Id,
                    PId = x.ParentId,
                    Value = x.Id,
                    Label = x.Name
                }).ToListAsync();
            return departments;
        }
    }
}
